﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Enums;

namespace Resto.CostControl.Core.Facade.MaterialModule.DataTransferObject
{
    /// <summary>
    /// 原材料加工单
    /// </summary>
    [Serializable]
    public class MaterialProcessDTO
    {
        #region 构造函数

        public MaterialProcessDTO() { }

        internal MaterialProcessDTO(MaterialProcess process)
        {
            this.ID = process.ID;
            this.Code = process.Code;
            this.ProcessDate = process.ProcessDate;
            this.OrderState = (MaterialProcessState)Enum.Parse(typeof(MaterialProcessState),process.OrderState);
            
            List<MaterialProcessItemDTO> list = new List<MaterialProcessItemDTO>();
            foreach (MaterialProcessItem item in process.Items)
                list.Add(new MaterialProcessItemDTO(item));

            this.Items = list.ToArray();

        }

        #endregion

        #region 基本属性

        public string ID { get; set; }

        /// <summary>
        /// 合同编号
        /// </summary>
        public string Code { get; set; }


        /// <summary>
        /// 加工日期
        /// </summary>
        public DateTime ProcessDate { get; set; }

        /// <summary>
        /// 加工单状态,UI不要赋值
        /// </summary>
        public MaterialProcessState OrderState { get; set; }

        public MaterialProcessItemDTO[] Items { get; set; }
               
        #endregion

        #region 方法

         internal MaterialProcess ToMaterialProcess()
        {
            MaterialProcess order = new MaterialProcess();
            CopyToMaterialProcess(order);
            return order;
        }

        internal void CopyToMaterialProcess(MaterialProcess order)
        {
            order.Code = this.Code;
            order.ProcessDate = this.ProcessDate;

            //清除要删除的项
            string[] oldIds = (from o in order.Items
                            select o.ID).ToArray();

            string[] newIds = (from o in this.Items
                            select o.ID).ToArray();

            List<string> delIds = (from o in oldIds
                            where !newIds.Contains(o)
                            select o).ToList();

            delIds.ForEach(x => order.RemoveItem(x));


            //修改和新增项
            foreach (MaterialProcessItemDTO itemDTO in Items)
            {
                MaterialProcessItem item;
                if (!string.IsNullOrWhiteSpace(itemDTO.ID))
                {
                    item = order.GetItem(itemDTO.ID);
                    if (item != null)
                        itemDTO.CopyToMaterialProcessItem(item);
                }
                else
                    order.AddItem(itemDTO.ToMaterialProcessItem());
            }
        }

        

        #endregion
    }
}

